package nc.tb.app.adjustbill.ui;

import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import nc.tb.pub.table.DimCellEditor;
import nc.ui.pub.beans.UITable;
import nc.ui.pub.beans.UITablePane;
import nc.ui.pub.beans.UITextField;
import nc.ui.pub.beans.ValueChangedListener;
import nc.ui.pub.beans.textfield.UITextType;

public class PlanAdjustCellTable extends UITablePane {
	
	private int tableType = PlanAdjustCellTableModel.tableType_Nomal;

	public PlanAdjustCellTable() {
		super();
		initialize();
	}
	public PlanAdjustCellTable(int type) {
		super();
		tableType = type;
		initialize();
	}
		
	private void initialize(){
		UITable table = getTable();
		PlanAdjustCellTableModel model = new PlanAdjustCellTableModel(tableType);
		table.setModel(model);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		for (int i=0; i<model.adjCols.length-1; i++) {
			int colNumber = model.adjCols[i];
			TableColumn col = table.getColumnModel().getColumn(colNumber);
			if (col != null) {
				UITextField text = new UITextField();
				text.setTextType(UITextType.TextDbl);
				if (tableType != PlanAdjustCellTableModel.tableType_Nomal && colNumber == model.adjCols[1]) {
					text.setDocument(new PlainDocument(){
						public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
							if (str == null || str.length() == 0)
								return;
							for (int i = 0; i < str.length(); i++) {
								char c = str.charAt(i);
								if (!(c >= '0' && c <= '9') && c != '.')
									return;
							}
							super.insertString(offset, str, attr);
						}
					});
				}
				DimCellEditor editor = new DimCellEditor(text);
				col.setCellEditor(editor);
			}
		}
	}
	
	public PlanAdjustCellTableModel getModel() {
		return (PlanAdjustCellTableModel)getTable().getModel();
	}
	
	public void addValueChangedListener(ValueChangedListener listener) {
		getModel().addValueChangedListener(listener);
	}
}
